package net.maku.im.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.im.constant.ImConstant;
import net.maku.im.convert.ImGroupMessageConvert;
import net.maku.im.dao.ImGroupMessageDao;
import net.maku.im.dto.ImGroupMessageQueryDTO;
import net.maku.im.entity.ImGroupMessageEntity;
import net.maku.im.service.ImGroupMessageService;
import net.maku.im.vo.ImGroupMessageVO;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 群聊消息
 *
 * @author Blue 
 * @since 1.0.0 2024-07-20
 */
@Service
@AllArgsConstructor
@DS(ImConstant.DS_IM_PLATFORM)
public class ImGroupMessageServiceImpl extends BaseServiceImpl<ImGroupMessageDao, ImGroupMessageEntity> implements ImGroupMessageService {

    @Override
    public PageResult<ImGroupMessageVO> page(ImGroupMessageQueryDTO query) {
        LambdaQueryWrapper<ImGroupMessageEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(!Objects.isNull(query.getSendId()), ImGroupMessageEntity::getSendId, query.getSendId());
        wrapper.eq(!Objects.isNull(query.getGroupId()), ImGroupMessageEntity::getGroupId, query.getGroupId());
        wrapper.eq(!Objects.isNull(query.getType()), ImGroupMessageEntity::getType, query.getType());
        wrapper.like(StrUtil.isNotEmpty(query.getContent()), ImGroupMessageEntity::getContent, query.getContent());
        if (CollectionUtil.isNotEmpty(query.getSendTime()) && query.getSendTime().size() > 1) {
            wrapper.between(ImGroupMessageEntity::getSendTime, query.getSendTime().get(0),
                query.getSendTime().get(1));
        }
        wrapper.orderByDesc(ImGroupMessageEntity::getId);
        IPage<ImGroupMessageEntity> page = baseMapper.selectPage(getPage(query), wrapper);
        return new PageResult<>(ImGroupMessageConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    @Override
    public ImGroupMessageVO info(Long id) {
       ImGroupMessageEntity entity = this.getById(id);
       ImGroupMessageVO vo = ImGroupMessageConvert.INSTANCE.convert(entity);
       return vo;
    }


}